package com.forthfactory;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class BeanFactory {
   /* //静态代码块，让这个工厂在new 出来的时候，就直接去读取配置文件
    static Map allObjectl;

    static {
        Properties properties = new Properties();
        allObjectl = new HashMap();
        try {
            properties.load(BeanFactory.class.getClassLoader().getResourceAsStream("spring.properties"));
            Enumeration<?> enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()) {
                //获取元素并转换成字符串
                String key = enumeration.nextElement() + "";
                System.out.println("key="+key);
                //通过key获取全限定类名
                String value = properties.getProperty(key);
                //通过反射创建对象实例
                Object o = Class.forName(value).newInstance();
                allObjectl.put(key,o);
            }
        } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }*/
    public Object getBean(String beanName){
        Properties properties = new Properties();
        Map allObjectl = new HashMap();
        try {
            properties.load(BeanFactory.class.getClassLoader().getResourceAsStream("spring.properties"));
            Enumeration<?> enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()) {
                //获取元素并转换成字符串
                String key = enumeration.nextElement() + "";
                System.out.println("key="+key);
                //通过key获取全限定类名
                String value = properties.getProperty(key);
                //通过反射创建对象实例
                Object o = Class.forName(value).newInstance();
                allObjectl.put(key,o);
            }
        } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
         return allObjectl.get(beanName);
    }
}






